package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.page.Page;
import com.dk.pojo.brand.TbBrand;
import com.dk.service.brand.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@Controller
@RequestMapping("brand")
public class BrandController {

    private  final Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference
    private BrandService brandService;

    @RequestMapping("listBrand")
    @ResponseBody
    public Result listBrand() {
        try {
            List<TbBrand> listBrand = brandService.listBrand();
            return Result.response(ResultEnum.OK).setData(listBrand);
        } catch (Exception e) {
            logger.error("错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("toBrand")
    public String toBrand(Model model) {

        return "admin/brand";
    }

    @RequestMapping("getListBrand")
    @ResponseBody
    public Page getListBrand(@RequestParam(defaultValue = "1") Integer pageNumber,
                                           @RequestParam(defaultValue = "3") Integer pageSize) {
        Page page = brandService.getListBrand(pageNumber,pageSize);
        return page;
    }

    @RequestMapping("addBrand")
    @ResponseBody
    public Result addBrand(TbBrand tbBrand) {
        try {
            int i = brandService.addBrand(tbBrand);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("品牌添加错误：{}",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    /**
     * 根据id获取brand
     * @param id
     * @return
     */
    @RequestMapping("getBrandById/{id}")
    @ResponseBody
    public Result getBrandById(@PathVariable("id") Long id) {
        try {
            TbBrand brand = brandService.getBrandById(id);
            return Result.response(ResultEnum.OK).setData(brand);

        } catch (Exception e) {
            logger.error("获取brand错误:" ,e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("updateBrandInfo")
    @ResponseBody
    public Result updateBrandInfo(TbBrand tbBrand) {
        try {
            int i = brandService.updateBrandById(tbBrand);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("修改brand错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("deleteBrandByIds")
    @ResponseBody
    public Result deleteBrandByIds(@RequestParam("ids[]")Long[] ids) {
        try {
            int i = brandService.deleteBrandByIds(ids);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("品牌删除错误:",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
